
#ifndef _HLINK_USER_API_H_
#define _HLINK_USER_API_H_


#define HLINK_SSID_LEN_MAX          32
#define HLINK_PWD_LEN_MAX           64
#define HLINK_GATEWAY_IP_LEN_MAX    46 //ip6 max 46

typedef struct {
    char ssid[HLINK_SSID_LEN_MAX+1];
    char pwd[HLINK_PWD_LEN_MAX+1];
    unsigned char flag;
    unsigned char reserved;
    unsigned short gatewayport;
    char gatewayip[HLINK_GATEWAY_IP_LEN_MAX];//include '\0',total 46
}hlink_module_conf;

typedef struct {
    int mid;
    hlink_module_conf conf;
}hlink_set_module_config_req;

typedef  struct {
    int mid;    
    int retcode;
}hlink_set_module_config_rsp;

typedef struct {
    int mid;    
}hlink_get_module_config_req;

typedef struct {
    int mid;
    int retcode;
    hlink_module_conf conf;
}hlink_get_module_config_rsp;

typedef enum {
    HLINK_SERVICE_CT_JSON                = 50,
    HLINK_SERVICE_CT_ASN1_UPER           = 20480,
    HLINK_SERVICE_CT_ASN1_DER            = 20481,
}accept_content_type;

#define HLINK_HIV_LEN_MAX           32
#define HLINK_FWV_LEN_MAX           64
#define HLINK_HWV_LEN_MAX           64
#define HLINK_SWV_LEN_MAX           64
typedef struct
{
  unsigned char  hiv[HLINK_HIV_LEN_MAX];
  unsigned char  fwv[HLINK_FWV_LEN_MAX];
  unsigned char  hwv[HLINK_HWV_LEN_MAX];
}moudle_online_info;


#define HLINK_SN_LEN_MAX              40
#define HLINK_MODEL_LEN_MAX           32
#define HLINK_MANU_LEN_MAX            32
#define HLINK_NAME_LEN_MAX            32
#define HLINK_DESC_LEN_MAX            64
#define HLINK_RFV_LEN_MAX             16
#define HLINK_MAC_LEN_MAX             32
#define HLINK_PROT_TYPE_LEN_MAX       16
typedef struct
{
    unsigned char  sn[HLINK_SN_LEN_MAX];               /* Required */
    unsigned char  model[HLINK_MODEL_LEN_MAX];         /* Required */
    unsigned char  manu[HLINK_MANU_LEN_MAX];           /* Required */
    unsigned char  name[HLINK_NAME_LEN_MAX];           /* Required */
    unsigned char  desc[HLINK_DESC_LEN_MAX];           /* Not Required */
    unsigned char  rfv[HLINK_RFV_LEN_MAX];             /* Not Required */
    unsigned char  mac[HLINK_MAC_LEN_MAX];             /* Not Required */
    unsigned char  fwv[HLINK_FWV_LEN_MAX];             /* Not Required */
    unsigned char  hwv[HLINK_HWV_LEN_MAX];             /* Not Required */
    unsigned char  swv[HLINK_SWV_LEN_MAX];             /* Not Required */
    unsigned char  protType[HLINK_PROT_TYPE_LEN_MAX];  /* Not Required */
    unsigned short accept_ct;                          /* Required, hlink service accept content type*/
}device_register_req;

#define HLINK_DEV_REG_SUCC    0
#define HLINK_DEV_REG_FAIL   -1
typedef struct
{
    int            errcode;
}device_register_rsp;


#define HLINK_FILE_OPERATE_CODE_LEN_MAX     32
#define HLINK_FILE_NAME_LEN_MAX             64
#define HLINK_FILE_TYPE_LEN_MAX             32
#define HLINK_FILE_VERSION_LEN_MAX          64
#define HLINK_FILE_CHECK_SUM_LEN_MAX        64
#define HLINK_FILE_ERR_DESC_LEM_MAX         64
typedef struct
{
    unsigned char  operate_code[HLINK_FILE_OPERATE_CODE_LEN_MAX];
    unsigned char  file_name[HLINK_FILE_NAME_LEN_MAX];
    unsigned char  file_type[HLINK_FILE_TYPE_LEN_MAX];
    unsigned int   file_size;
    unsigned char  file_version[HLINK_FILE_VERSION_LEN_MAX];
    unsigned char  check_sum[HLINK_FILE_CHECK_SUM_LEN_MAX]; /* SHA256 */
}file_mngr_notice;

typedef struct
{
    int  errcode;
    char err_desc[HLINK_FILE_ERR_DESC_LEM_MAX];
    char file_name[HLINK_FILE_NAME_LEN_MAX];
    char file_type[HLINK_FILE_TYPE_LEN_MAX];
    int  file_size;
    char file_version[HLINK_FILE_VERSION_LEN_MAX];
    char check_sum[HLINK_FILE_CHECK_SUM_LEN_MAX];// sha256
}file_mngr_result;

typedef struct
{
    int  errcode;
    char status[HLINK_FILE_ERR_DESC_LEM_MAX];
    char file_name[HLINK_FILE_NAME_LEN_MAX];
    int  file_size;
}file_mngr_result_app;


typedef struct
{
    int  message_id;
    char operate_code[HLINK_FILE_OPERATE_CODE_LEN_MAX];
    char device_name[HLINK_NAME_LEN_MAX];
}device_manager;

typedef struct
{
    int  message_id;
    int  errcode;
    char errDesc[HLINK_DESC_LEN_MAX];
}device_manager_rsp;

/* error code */
typedef enum{
    HLINK_OK,                                               /* 成功 */
    HLINK_ERR_COMMON_ERROR,                                 /* 通用错误 */
    HLINK_ERR_SESSION_DENY,                                 /* 创建会话被拒绝，网关不允许新的连接 */
    HLINK_ERR_SESSION_NOT_EXIST,                            /* 会话不存在 */
    HLINK_ERR_INVALID_PARA,                                 /* 无效参数 */
    HLINK_ERR_INVALID_FORMAT,                               /* 无效数据格式 */
    HLINK_ERR_INVALID_OPTION,                               /* 无效option，未携带正确的option */
    HLINK_ERR_WHITELIST_CHECK_ERROR,                        /* 白名单校验失败 */
    HLINK_ERR_SYSTEM_BUSY,                                  /* 系统正忙，系统正在处理其他任务时，无法响应新请求 */
    HLINK_ERR_SYSTEM_RES_NOT_ENOUGH,                        /* 系统资源不足 */
    HLINK_ERR_TIME_OUT,                                     /* 超时 */
    HLINK_ERR_FILE_NOT_EXIST = 21,                          /* 文件不存在 */
    HLINK_ERR_FILE_NO_SPACE,                                /* 空间不足 */
    HLINK_ERR_FILE_CHECKSUM_ERROR,                          /* 文件校验错误 */
    HLINK_ERR_FILE_NAME_ERROR,                              /* 文件名称错误 */
    HLINK_ERR_FILE_SIZE_ERROR,                              /* 文件大小错误 */
    HLINK_ERR_FILE_FORMAT_ERROR,                            /* 文件格式错误 */
    HLINK_ERR_FILE_CONNECT_ERROR,                           /* 文件传输通道连接错误 */
    HLINK_ERR_FILE_TRANS_ERROR,                             /* 文件传输错误 */
    HLINK_WAIT_DEVICE_RSP_TIMEOUT = 40,                     /* 等待设备回应超时 */
    HLINK_ERR_WITHOUT_AUTH = 99,                            /* 未鉴权 */
    /*配网错误码 100*/
    /* HLINK_ERR_PARA_INVALID = 100,                           输入参数错误, 废弃，用HLINK_ERR_INVALID_PARA代替 */
    HLINK_ERR_INNER_FAILED = 101,                           /* 内部处理失败 */
    HLINK_ERR_SAVE_SSID_FAILED,                             /* 配置SSID失败 */
    HLINK_ERR_SAVE_GWIP_FAILED,                             /* 配置网关失败 */
    HLINK_ERR_READ_SSID_FAILED,                             /* 读取SSID失败 */
    HLINK_ERR_SSID_NOT_EXIST,                               /* SSID不存在 */
    HLINK_ERR_READ_GWIP_FAILED,                             /* 读取网关数据失败 */
    /*APP登录认证错误码 110*/
    HLINK_ERR_READ_DATA_FAIL = 112,                          /* 读取数据失败 */
    HLINK_ERR_WRITE_DATA_FAIL,                               /* 写入数据失败 */
    HLINK_ERR_ENCRYPT_FAILED,                                /* 加密失败 */
    HLINK_ERR_DECRYPT_FAILED,                                /* 解密失败 */
    HLINK_ERR_OLD_PWD_NOT_MATCHING,                          /* 与旧密码不匹配 */
    HLINK_ERR_APP_AUTH_FAIL,                                 /* 认证失败 */
    /* HLINK_ERR_FAILED_UNKNOWN,                                未知错误原因，废弃，用HLINK_ERR_COMMON_ERROR代替 */
    /*固件升级错误码 120*/
    HLINK_ERR_UPGRADE_PACKAGE_ERROR = 120,                   /* Package包错误 */
    HLINK_ERR_UPGRADE_FAILED,                                /* Package升级失败 */
    /*下发证书错误码 130*/
    HLINK_ERR_CERTIFICATE_EXPIRATION = 130,                   /* 证书过期 */
}hlink_errno;


#define HLINK_APP_PWD_NOT_EXIST            1
#define HLINK_APP_PWD_EXIST                2

/* error code information */
#define HLINK_ERR_INFO_COMMON_ERROR                          "Common error"
#define HLINK_ERR_INFO_SESSION_DENY                          "Session is denied"
#define HLINK_ERR_INFO_SESSION_NOT_EXIST                     "Session does not exist"
#define HLINK_ERR_INFO_INVALID_PARA                          "Invalid parameter"
#define HLINK_ERR_INFO_INVALID_FORMAT                        "Invalid format"
#define HLINK_ERR_INFO_INVALID_CT_FORMAT                     "Content format is not OMIP_CONTENT_FORMAT_BINARY"
#define HLINK_ERR_INFO_INVALID_OPTION                        "Invalid option"
#define HLINK_ERR_INFO_WHITELIST_CHECK_ERROR                 "Check white list error"
#define HLINK_ERR_INFO_SYSTEM_BUSY                           "System is busy now"
#define HLINK_ERR_INFO_FILE_NOT_EXIST                        "File does not exist"
#define HLINK_ERR_INFO_FILE_NO_SPACE                         "File has no any space"
#define HLINK_ERR_INFO_FILE_CHECKSUM_ERROR                   "File checksum is wrong"
#define HLINK_ERR_INFO_FILE_NAME_ERROR                       "File name is wrong"
#define HLINK_ERR_INFO_FILE_SIZE_ERROR                       "File size is wrong"
#define HLINK_ERR_INFO_FILE_FORMAT_ERROR                     "File format is wrong"
#define HLINK_ERR_INFO_FILE_CONNECT_ERROR                    "Connect error in file transfer"
#define HLINK_ERR_INFO_UNKNOWN                               "Unknown error"
#define HLINK_ERR_INFO_CREATE_JSON_OBJECT                    "Create json object error"
#define HLINK_ERR_INFO_CONVERT_JSON_TO_STRING                "Convert json to string error"
#define HLINK_ERR_INFO_ENQUEUE_RESPONSE_MSG                  "Enqueue response message error"
#define HLINK_ERR_INFO_COPY                                  "Copy error"
#define HLINK_ERR_INFO_COPY_PASSWORD                         "Copy password error"
#define HLINK_ERR_INFO_COPY_OPERATE_CODE                     "Copy operate code error"
#define HLINK_ERR_INFO_COPY_DEVICE_NAME                      "Copy device name error"
#define HLINK_ERR_INFO_COPY_CERT_DOMAIN                      "Copy certificate domain error"
#define HLINK_ERR_INFO_COPY_ERROR_DESC                       "Copy error description error"
#define HLINK_ERR_INFO_COPY_FILE_TYPE                        "Copy file type error"
#define HLINK_ERR_INFO_COPY_FILE_NAME                        "Copy file name error"
#define HLINK_ERR_INFO_COPY_FILE_VERSION                     "Copy file version error"
#define HLINK_ERR_INFO_COPY_FILE_CHECKSUM                    "Copy file checksum error"
#define HLINK_ERR_INFO_GET_MOBILE_APP_AUTH_ZONE              "Get zone of mobile APP authentication error"
#define HLINK_ERR_INFO_GET_CERT_ZONE                         "Get zone of certificate error"
#define HLINK_ERR_INFO_GET_FILEMNGR_ZONE                     "Get zone of file manage error"
#define HLINK_ERR_INFO_GET_LOGRECORD_ZONE                    "Get zone of log record error"
#define HLINK_ERR_INFO_GET_CERT_BUFFER                       "Get buffer of certificate error"
#define HLINK_ERR_INFO_GET_JSON_DEVPWD_ITEM                  "Get json item of devPwd error"
#define HLINK_ERR_INFO_GET_HLINK_STATISTICS_JSON_FAILED      "Get json of hlink statistics error"
#define HLINK_ERR_INFO_UNKNOWN_OPERATE_CODE                  "Unknown operate code"
#define HLINK_ERR_INFO_UNKNOWN_OPERATE_OBJECT                "Unknown operate object"
#define HLINK_ERR_INFO_UNKNOWN_CERT_TYPE                     "Unknown certificate type"
#define HLINK_ERR_INFO_UNKNOWN_UPGRADE_RESULT                "Unknown upgrade result"
#define HLINK_ERR_INFO_STORE_DATA_TO_ZONE_ERROR              "Store data to zone error"
#define HLINK_ERR_INFO_STORE_DATA_TO_CERT_ZONE_ERROR         "Store data to zone of certificate error"
#define HLINK_ERR_INFO_STORE_DATA_TO_FILEMNGR_ZONE_ERROR     "Store data to zone of file manage error"
#define HLINK_ERR_INFO_SOTRE_PASSWORD                        "Store password error"
#define HLINK_ERR_INFO_CLEAR_FLASH_PASSWORD                  "Clear password in flash error"
#define HLINK_ERR_INFO_ALLOCATE_CERT_MEMORY                  "Allocate memory of certificate error"
#define HLINK_ERR_INFO_ALLOCATE_MEMORY                       "Allocate memory error"
#define HLINK_ERR_INFO_READ_CERT_ZONE                        "Read data from zone of certificate error"
#define HLINK_ERR_INFO_READ_CERT                             "Read certificate error"
#define HLINK_ERR_INFO_READ_LOG                              "Read log error"
#define HLINK_ERR_INFO_READ_SOCKET                           "Receive from network error"
#define HLINK_ERR_INFO_WRITE_CERT                            "Write certificate error"
#define HLINK_ERR_INFO_WRITE_SOCKET                          "Send to network error"
#define HLINK_ERR_INFO_DECRYPT_CERT                          "Decrypt certificate error"
#define HLINK_ERR_INFO_TOO_LARGE_DATA                        "Data too large"
#define HLINK_ERR_INFO_ERASE_FILEMNGR_ZONE                   "Erase zone of file manage error"
#define HLINK_ERR_INFO_ERASE_CERT_ZONE                       "Erase zone of certificate error"
#define HLINK_ERR_INFO_CONNECT_GATEWAY                       "Connect gateway error"
#define HLINK_ERR_INFO_SEND_FILE_DATA_TO_DDC                 "Send file to DDC error"
#define HLINK_ERR_INFO_SEND_FILE_END_FLAG_TO_DDC             "Send file end flag to DDC error"
#define HLINK_ERR_INFO_NO_LOG_TO_UPLOAD                      "No log to upload"
#define HLINK_ERR_INFO_UPGRADE_SUBFILE_ERROR                 "Upgrade subfile error"
#define HLINK_ERR_INFO_UPGRADE_WORK_KEY_FAILED               "Upgrade work key error"
#define HLINK_ERR_INFO_OTA_UPGRADE_FAILED                    "Upgrade OTA error"
#define HLINK_ERR_INFO_FORMAT_LOG_FAILED                     "Format Log error"
#define HLINK_ERR_INFO_INVALID_LENGTH                        "Invalid length"
#define HLINK_ERR_INFO_INVALID_OPERATE_CODE                  "Invalid operate code"

#define HLINK_ERR_INFO_GET_CLIENT_INFO_FAILED                "get client info failed"
#define HLINK_ERR_INFO_UPDATE_CERT_DOMAIN_FAILED             "update cert domain failed"
#define HLINK_MSG_FILE_TRANSFER_OK                           "File transfer is success"


#define BLE_VERSION_REPORT       0

typedef struct ble_dev_mng_req_para{
    int operate;
    int mid;
    int reserve;
    int payload_len;
    char payload[128];
} ble_dev_mng_req;


typedef enum{
    UPGRADE_OK_REBOOT = 0,
    UPGRADE_OK_NOT_REBOOT,
    UPGRADE_ERR_NOT_REBOOT,
}hlink_upgrade;

#define HLINK_MAX_APP_PWD_LEN         32
#define HLINK_MAX_APP_PWD_SHA256_LEN  (HLINK_MAX_APP_PWD_LEN*2)

#define HLINK_DISCOVERY_STRING  "discovery"
#define HLINK_SETPWD_STRING     "setPwd"
#define HLINK_SHOWPWD_STRING    "showPwd"

#define HLINK_MAX_CHALLENGE_LEN    32

#define HLINK_APP_DISCOVERY   0
#define HLINK_APP_SET_PWD     1
#define HLINK_APP_MODIFY_PWD  2
#define HLINK_APP_LOGIN       3
#define HLINK_APP_DEL_PWD     4

typedef struct {
    char new_pwd[HLINK_MAX_APP_PWD_LEN+1];
    char old_pwd[HLINK_MAX_APP_PWD_LEN+1];
}app_pwds;
typedef struct {
    int operate;
    int mid;
    union {
        app_pwds s_set;                      //modify
        char s_pwd[HLINK_MAX_APP_PWD_LEN+1]; //new register
        char s_pwd_sha256[HLINK_MAX_APP_PWD_SHA256_LEN+1]; //login
    }u;   
}hlink_app_security_req;


typedef struct {
    int operate;
    int mid;
    int result;
    char challenge[HLINK_MAX_CHALLENGE_LEN+1];
}hlink_app_security_rsp;

#define HLINK_APP_AUTH_INIT   0
#define HLINK_APP_AUTH_FAILED 1
#define HLINK_APP_AUTH_PASS   2
#define HLINK_APP_AUTH_CLOSED 3

typedef struct {
    int auth_code;
}app_session;

#define HLINK_ONLINE         1
#define HLINK_OFFLINE        2
#define HLINK_REGISTERED     3

typedef struct
{
	unsigned char status;
	unsigned char seq;
	unsigned short res;
}device_beatheart;


#endif
